﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FanPages.Server.Repostory.EFRepository.Model;
using FanPages.Server.Services.Contracts.Dto;
using FanPages.Common;

namespace FanPages.Server.Services.Transformers
{
    public class SearchExceptionTransformerSvc : IServerTransformer<SearchException, SearchExceptionDto>
    {
        public SearchException ToExistingClient(SearchExceptionDto dto, SearchException entity)
        {
            entity.Id = dto.Id;
            entity.Exception = dto.Exception;
            entity.FanItemReference.EntityKey = new System.Data.EntityKey("FanPagesEntities.FanItem", "Id", dto.FanItem.Id);
            return entity;
        }

        public SearchExceptionDto ToDto(SearchException entity)
        {
            var dto = new SearchExceptionDto()
            {
                Exception = entity.Exception,
                Id = entity.Id,
                FanItem = new FanItemTransformerSvc().ToDto(entity.FanItem)
            };
            return dto;
        }

        public SearchException ToClient(SearchExceptionDto dto)
        {
            var entity = new SearchException()
            {
                Id = dto.Id,
                Exception = dto.Exception                              
            };
            entity.FanItemReference.EntityKey = new System.Data.EntityKey("FanPagesEntities.FanItem", "Id", dto.FanItem.Id);
            return entity;
        }

        public List<SearchExceptionDto> ToListDto(List<SearchException> list)
        {
            var newList = new List<SearchExceptionDto>();
            list.ForEach(x =>
            {
                newList.Add(ToDto(x));
            });

            return newList;
        }

        public List<SearchException> ToListEntity(List<SearchExceptionDto> list)
        {
            var newList = new List<SearchException>();
            list.ForEach(x =>
            {
                newList.Add(ToClient(x));
            });

            return newList;
        }
    }
}
